package baseObjectContract;

import assertTools.DecoratorTools;
import baseObject.BaseObjectsFactory;
import baseObject.IBrickBreaker;
import baseObject.IPaddle;
import baseObjectDecorator.PaddleDecorator;

public class PaddleContract extends PaddleDecorator{
	public IPaddle paddle;
	
	public PaddleContract(IPaddle paddle) {
		super(paddle);
		this.paddle = paddle;
	}

	public void init(int x, int y, int width, int height){
		DecoratorTools.checkPre(x >= 0 && y >= 0 && width >= 0 && height >= 0);
		DecoratorTools.checkPre(x + width <= IBrickBreaker.XMAX || y + height <= IBrickBreaker.YMAX);
		
		this.paddle = BaseObjectsFactory.newPaddle(x, y, width, height);
		
		DecoratorTools.checkPost(this.paddle.getX() == x && this.paddle.getY() == y);
	}

	public void left() {
		IPaddle paddleAtOld = (IPaddle) this.paddle.cloneObj();
		
		DecoratorTools.checkPre(this.paddle.getX() >= 0);
		
		super.left();
		if(this.paddle.getX() > 0)
			DecoratorTools.checkPost(this.paddle.getX() == paddleAtOld.getX() - IPaddle.STEP);
			
	}


	public void right() {
		IPaddle paddleAtOld = (IPaddle) this.paddle.cloneObj();
		
		DecoratorTools.checkPre(this.paddle.getX() + this.paddle.getWidth() <= IBrickBreaker.XMAX);
		
		super.right();
		if(this.paddle.getX() + this.paddle.getWidth() < IBrickBreaker.XMAX)
			DecoratorTools.checkPost(this.paddle.getX() == paddleAtOld.getX() + IPaddle.STEP);
	}

	@Override
	public Object cloneObj() {
		return this.cloneObj();
	}

}
